package com.wzg.mydkvideoplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videocontroller.component.CompleteView;
import xyz.doikki.videocontroller.component.ErrorView;
import xyz.doikki.videocontroller.component.GestureView;
import xyz.doikki.videocontroller.component.LiveControlView;
import xyz.doikki.videocontroller.component.PrepareView;
import xyz.doikki.videocontroller.component.TitleView;
import xyz.doikki.videocontroller.component.VodControlView;
import xyz.doikki.videoplayer.exo.ExoMediaPlayer;
import xyz.doikki.videoplayer.exo.ExoMediaPlayerFactory;
import xyz.doikki.videoplayer.player.VideoView;

public class MainActivity extends AppCompatActivity {


    private VideoView mVideoView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("dk video player  test");


        mVideoView = findViewById(R.id.player);


        startPlayer("www",
                "http://vfx.mtime.cn/Video/2019/03/14/mp4/190314223540373995.mp4",
                "https://cms-bucket.nosdn.127.net/eb411c2810f04ffa8aaafc42052b233820180418095416.jpeg",
                false
        );


    }


    @Override
    protected void onPause() {
        super.onPause();
        //如果视频还在准备就 activity 就进入了后台，建议直接将 VideoView release
        //防止进入后台后视频还在播放
        if (mVideoView != null && mVideoView.getCurrentPlayerState() == VideoView.STATE_PREPARING) {
            mVideoView.release();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mVideoView != null) {
            mVideoView.resume();
        }
    }

    @Override
    public void onBackPressed() {
        if (mVideoView != null && !mVideoView.onBackPressed()) {
            super.onBackPressed();
        }
    }


    /**
     * @param title    标题
     * @param videoUrl 视频地址
     * @param thumbUrl 封面
     * @param isLive   是否为直播
     */
    private void startPlayer(String title, String videoUrl, String thumbUrl, boolean isLive) {

        if (mVideoView == null) return;

        Intent intent = getIntent();
        if (intent == null) return;


        StandardVideoController playerController = new StandardVideoController(this);
        //根据屏幕重心是否全屏
        playerController.setEnableOrientation(true);

        PrepareView prepareView = new PrepareView(this);
        prepareView.setClickStart();

        //封面图
        final ImageView thumb = prepareView.findViewById(R.id.thumb);
        Glide.with(this)
                .load(thumbUrl)
                .into(thumb);

        playerController.addControlComponent(prepareView);
        playerController.addControlComponent(new CompleteView(this));//自动完成播放界面
        playerController.addControlComponent(new ErrorView(this));//错误界面

        //设置标题
        final TitleView titleView = new TitleView(this);
        titleView.setTitle(title);
        playerController.addControlComponent(titleView);

        //是否为直播
        if (isLive) {
            playerController.addControlComponent(new LiveControlView(this));//直播控制条
        } else {
            VodControlView vodControlView = new VodControlView(this);
            //是否显示底部进度条，默认显示
//            vodControlView.showBottomProgress(false);
            playerController.addControlComponent(vodControlView);

        }
        //滑动控制视图
        GestureView gestureView = new GestureView(this);
        playerController.addControlComponent(gestureView);

        //根据是否为直播决定是否需要滑动调节进度
        playerController.setCanChangePosition(!isLive);

        //竖屏也开启手势操作，默认关闭
//            controller.setEnableInNormal(true)
        //滑动调节亮度，音量，进度，默认开启
//            controller.setGestureEnabled(false)
        //适配刘海屏，默认开启
//            controller.setAdaptCutout(false)
        //双击播放暂停，默认开启
//            controller.setDoubleTapTogglePlayEnabled(false)

        //在控制器上显示调试信息
//        playerController.addControlComponent(new DebugInfoView(this));
        //在LogCat显示调试信息
        playerController.addControlComponent(new PlayerMonitor());

        //如果你不想要UI，不要设置控制器即可
        mVideoView.setVideoController(playerController);
        mVideoView.setUrl(videoUrl);
        //保存播放进度
        mVideoView.setProgressManager(new ProgressManagerImpl());
        //播放状态监听
        mVideoView.addOnStateChangeListener(mOnStateChangeListener);
        mVideoView.setPlayerFactory(ExoMediaPlayerFactory.create());
        mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_16_9);

        //设置静音播放
//            mVideoView.setMute(true)

        //从设置的position开始播放
//            mVideoView.skipPositionWhenPlay(10000)
//        mVideoView.start();


    }


    private final VideoView.OnStateChangeListener mOnStateChangeListener = new VideoView.OnStateChangeListener() {
        @Override
        public void onPlayerStateChanged(int playerState) {

            if (playerState == VideoView.PLAYER_NORMAL) {

            }

            if (playerState == VideoView.PLAYER_FULL_SCREEN) {

            }

        }

        @Override
        public void onPlayStateChanged(int playState) {
            switch (playState) {
                case VideoView.STATE_IDLE: {
                }
                break;
                case VideoView.STATE_PREPARING: {
                }
                break;
                case VideoView.STATE_PREPARED: {
                }
                break;
                case VideoView.STATE_PLAYING: {
                    //需在此时获取视频宽高
                    int[] videoSize = mVideoView.getVideoSize();
//                    L.d("视频宽：" + videoSize[0])
//                    L.d("视频高：" + videoSize[1])
                }
                break;
                case VideoView.STATE_PAUSED: {
                }
                break;
                case VideoView.STATE_BUFFERING: {
                }
                break;
                case VideoView.STATE_BUFFERED: {
                }
                break;
                case VideoView.STATE_PLAYBACK_COMPLETED: {
                }
                break;
                case VideoView.STATE_ERROR: {
                }
                break;
            }

        }
    };
}